import React, { useEffect } from 'react'
import { Button, Form, Input, Card,message } from 'antd';
import { useNavigate, useParams } from 'react-router-dom';
import style from '../index.module.css'
import {addHospitalRequest} from '@api/hospital/hospitalSet'
import type {addHospitalParamType,updateHospitalFromIdParamType} from '@/api/hospital/model/hospitalSetTypes'
import { getHospitalFromIdRequest ,updateHospitalRequest} from '@api/hospital/hospitalSet';
export default function AddOrUpdateHosSet() {
  const navigate = useNavigate()
  const {id} = useParams()
  const [form] = Form.useForm()
  useEffect(() => {
    if(id){
      const getHospitalFromId = async () => {
        const re = await getHospitalFromIdRequest(id)
        form.setFieldsValue(re)
      }
      getHospitalFromId()
    }
  })
  
  //新增医院设置事件回调函数
  const addHosFinishHandle = async (HosFinishValue:addHospitalParamType) => {
    if(id){
      await updateHospitalRequest({...HosFinishValue,id}) 
      navigate('/syt/hospital/hospitalSet')   
      message.success("修改数据成功")
    }else{
      await addHospitalRequest(HosFinishValue)
      navigate('/syt/hospital/hospitalSet')
      message.success("新增数据成功")
    }   
  }
  //返回回调函数
  const goBackHandle = () => {
    navigate(-1)
  }
  return (
    <Card>
      <Form
        name="basic"
        labelCol={{ span: 3 }}
        wrapperCol={{ span: 18 }}
        onFinish={addHosFinishHandle}
        form={form}
      >
        <Form.Item
          label="医院名称"
          name="hosname"
          rules={[{
            required: true,
            message: '请输入医院名称',
            transform(value) {
              return value.trim()
            }
          }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="医院编号"
          name="hoscode"
          rules={[{
            required: true,
            message: '请输入医院编号',
            transform(value) {
              return value.trim()
            }
           
          }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="api基础路径"
          name="apiUrl"
          rules={[{
            required: true,
            message: '请输入api基础路径',
            
          }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="联系人姓名"
          name="contactsName"
          rules={[{
            required: true,
            message: '请输入联系人姓名',
            
          }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="联系人手机"
          name="contactsPhone"
          rules={[{
            required: true,
            message: '请输入联系人手机',
            
          }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          wrapperCol={{ offset: 3 }}
        >
          <Button type="primary" htmlType="submit" className={style.mr}>
            提交
          </Button>
          <Button type="default" htmlType="button" onClick={goBackHandle}>
            返回
          </Button>
        </Form.Item>
      </Form>
    </Card>
  );
}

